home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / gpp-1_42.lha / g++-1.42.0 / init_main.c < prev    next >
C/C++ Source or Header  |  1991-10-19  |  4KB  |  129 lines

  1. /* Startup code needed by GCC C++ output code.  */
  2. /* Copyright (C) 1991 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU CC.
  5.  
  6. GNU CC is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  11. GNU CC is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU CC; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /* As a special exception, if you link this file with files
  21.    compiled with GCC to produce an executable, this does not cause
  22.    the resulting executable to be covered by the GNU General Public License.
  23.    This exception does not however invalidate any other reasons why
  24.    the executable file might be covered by the GNU General Public License.  */
  25.  
  26. /*  init_main.c: Originally developed by James Kempf for SUN,
  27.  *               adapted and commented for FSF by Heinz Seidl (hgs@cygnus.com)
  28.  */
  29.  
  30. #include <assert.h>
  31. #include "init_main.h"
  32. #include "config.h"
  33.  
  34. /* define NO_ATEXIT in config.h if there isn't one */
  35.  
  36. #ifndef NO_ATEXIT
  37.   #define ATEXIT(FCTNP) 0
  38. #else
  39.   #ifdef sun
  40.     extern int on_exit( void (*)(), int); 
  41.     #define ATEXIT(FCTNP) on_exit( FCTNP, 0)
  42.   #else
  43.     extern int atexit( void (*)()); /* defined in stdlib.h */
  44.     #define ATEXIT(FCTNP) atexit( FCTNP)
  45.   #endif
  46. #endif /* NO_ATEXIT
  47.  
  48. /* 
  49.  * The main module and all shared modules have each their own __C/DTOR_LIST__.
  50.  * When a module is to be final/initialized, `__function_list_addr' gets the
  51.  * address of the  __C/DTOR_LIST__ of that module.
  52.  * (For the main module we can simple take the globals __C/DTOR_LIST__, for
  53.  * shared objects, we have to use functionality of the runtime linker ld.so
  54.  * to get the addresses. All this has to be done in the main program, since
  55.  * a lot of functionality of ld.so does not work as documented).
  56.  */
  57.  
  58. ep_fp * __function_list_addr = 0;
  59.  
  60. extern ep_fp __CTOR_LIST__[];
  61. extern ep_fp __DTOR_LIST__[];
  62.  
  63. int __main();
  64. void INITIALIZE_MODULE();
  65. void FINALIZE_MODULE();
  66. void __initialize_libraries();
  67. void __finalize_libraries();
  68. void exit(/*int*/);
  69. void _exit(/*int*/);
  70. void _cleanup();
  71.  
  72. entry_pt INIT_START;
  73. entry_pt INIT_END;
  74.  
  75.  
  76. /*************************************************************************
  77.  * Main initialization and finalization
  78.  ************************************************************************/
  79.  
  80. /* 
  81.  * Initialize first (dynamically linked) libraries, then the main module.
  82.  * Finalize first the main module then (dynamically linked) libraries.
  83.  */
  84.  
  85.  
  86.  
  87. void __initialize_main()
  88. {
  89.     __function_list_addr = __CTOR_LIST__;
  90.     INITIALIZE_MODULE();
  91. }
  92.  
  93. void __finalize_main()
  94. {
  95.     __function_list_addr = __DTOR_LIST__;
  96.     FINALIZE_MODULE();
  97. }
  98.  
  99.  
  100. int __main()
  101. {
  102.     int ret = 0;
  103.  
  104.     INIT_START(); /* user supplied function */
  105.  
  106.     __initialize_libraries();             /* first the libraries */
  107.     __initialize_main();          /* second main module */
  108.  
  109.     INIT_END();   /* user supplied function */
  110.  
  111.     ret = ATEXIT( __finalize_main);      assert( ret == 0);
  112.     ret = ATEXIT( __finalize_libraries); assert( ret == 0);
  113.     return(ret);
  114. }
  115.  
  116.  
  117. #ifndef HAVE_ATEXIT
  118. void exit( int status)
  119. {
  120.     __finalize_main();                   /* first main module */
  121.     __finalize_libraries();               /* second the libraries */
  122. #ifdef GPROF
  123.     _mcleanup();
  124. #endif
  125.     _cleanup();
  126.     _exit(status);
  127. }
  128. #endif
  129.